define(function (require, exports, module) {
    var template = require("template");
    var Ajax = require("ajax");
    var layer = require("layer");
    var util = require('util');
    var DropDown = require('dropdown');
	require('dropdownCss');
	require('validationJs');
	require('validationRule');
	require('validationCss');
    
    var submitObj = function (callback) {
        this.init.apply(this,arguments);
    };
    submitObj.prototype = {
        constructor: submitObj,
        options: {},
        init: function (options) {
            var t = this;
            t.options = $.extend({},t.options,options);
            $(".tipConNav").text("发起报送");
            $(window).off('scroll');
            $("#rightConBox").html(template("submitTemp"));
            t.mask();
            t.loadInitList();
            t.bindNav();
			//滚动条置顶
			window.scrollTo(0,0);
        },
        
		bindNav : function() {
			var t = this;
			if ($.cookie("roleEnum") == "user_com") {
				//submit,submit_pass,submit_audit
				$(".topNav #submit").remove();
				$(".topNav #submitPass").remove();
			}else if($.cookie("roleEnum") == "user_simple"){
				$(".topNav #submitAudit").remove();
			}
		},
		
        mask: function(){
            $(".mask").css("height",$(document).height());
            $(".mask").css("width",$(document).width());
        },
        
        loadInitList: function() {
			var t = this;
			var type_select = new DropDown($('#type_select')); //选择舆情类型
			var depart_select = new DropDown($('#depart_select')); //选择部门
			var leader_select = new DropDown($('#leader_select')); //选择领导
			var grade_select = new DropDown($('#grade_select')); //选择星级
			
			$("#type_select").one('click',function(){
	        	Ajax.ajax({
	        		url: localhost + 'information/selectBycompanyId.do',
	                success: function (data) {
	                	$("#type_select .dropdown").html('');
	                	if(data.returnCode==10000){
		                   if(data.returnData.length>0){
		                	   var item=data.returnData;
		                	   for(var i=0;i<item.length;i++){
		                		   $("#type_select .dropdown").append(template("submitTypeLi",item[i]));
		                	   }
		                	   t.bindTypeLiEvent();
		                   }else{
		                	   $("#type_select .dropdown").append('<li>暂无内容</li>');
		                   }
	                	}
	                },
	            });
			});
			
			$("#depart_select").one('click',function(){
	        	Ajax.ajax({
	        		url: localhost + 'submit/queryDepart.do',
	                success: function (data) {
	                	$("#depart_select .dropdown").html('');
	                	if(data.returnCode==10000){
		                   if(data.returnData.length>0){
		                	   var item=data.returnData;
		                	   for(var i=0;i<item.length;i++){
		                		   $("#depart_select .dropdown").append(template("submitDepartLi",item[i]));
		                	   }
		                	   t.bindDepartLiEvent();
		                   }else{
		                	   $("#depart_select .dropdown").append('<li>暂无内容</li>');
		                   }
	                	}
	                },
	            });
			});
			
			$("#leader_select").on('click',function(){
				var isDepartId = $("#departments").attr("data-departid"), departId='';
				if(isDepartId){
					$('.prev_depart').css('display','block');
					departId = isDepartId;
				}else{
					$('.prev_depart').css('display','none');
					layer.msg("请先选择报送部门！");
					return false;
				}
	        	Ajax.ajax({
	        		url: localhost + 'submit/queryUser.do',
	        		data:{departId:departId},
	                success: function (data) {
	                	$("#leader_select .dropdown").html('');
	                	if(data.returnCode==10000){
		                   if(data.returnData.length>0){
		                	   var item=data.returnData;
		                	   for(var i=0;i<item.length;i++){
		                		   $("#leader_select .dropdown").append(template("submitLeaderLi",item[i]));
		                	   }
		                	   t.bindLeaderLiEvent();
		                   }else{
		                	   $("#leader_select .dropdown").append('<li>暂无内容</li>');
		                   }
	                	}
	                },
	            });
			});
			
			$("#grade_select").on('click',function(){
				t.bindGradeLiEvent()
			});
			
			$(document).click(function() {
				// all dropdowns
				$('.wrapper-dropdown').removeClass('active');
			});
			
			$("#content_submit").on('click',function(){
				var submitTitle = $("#title_value").val()?$("#title_value").val().trim():null;
				var source = $("#source_value").val()?$("#source_value").val().trim():null;
				var informationId = $("#types").attr('data-ifmId');
				var receiveDepartId = $("#departments").attr('data-departId');
				var receiveUserId = $("#leaders").attr('data-userId');
				var submitLevel = $("#grades").attr('data-grade');
				var submitContent = $("#content_content_text").val()?$("#content_content_text").val().trim():null;
				
				if(!submitTitle){
					layer.msg("报送标题不能为空！");return false;
				}else if(submitTitle.length>30){
					layer.msg("报送标题限于30字符！");return false;
				}else if(!source){
					layer.msg("舆情来源不能为空！");return false;
				}else if(source.length>30){
					layer.msg("舆情来源限于30字符！");return false;
				}else if(!informationId){
					layer.msg("请选择舆情分类！");return false;
				}else if(!receiveDepartId){
					layer.msg("请选择报送部门！");return false;
				}else if(!receiveUserId){
					layer.msg("请选择报送领导！");return false;
				}else if(!submitLevel){
					layer.msg("请选择舆情等级！");return false;
				}else if(!submitContent){
					layer.msg("报送正文不能为空！");return false;
				}else if(submitContent.length>500){
					layer.msg("报送正文限于500字符！");return false;
				}
				
				var params={
					submitTitle : submitTitle,
					source : source,
					informationId : informationId,
					receiveDepartId : receiveDepartId,
					receiveUserId : receiveUserId,
					submitLevel : submitLevel,
					submitContent : submitContent	
				}
				
	        	Ajax.ajax({
	        		url: localhost + 'submit/post.do',
	        		data: params,
	                success: function (data) {
	                	$("#leader_select .dropdown").html('');
	                	if(data.returnCode==10000){
	                		layer.msg(data.returnMessage);
	                		window.location.href="#!admin/submit_pass";
	                	}
	                },
	            });
				
				
			});
			
		},
		
		bindTypeLiEvent: function(){
        	var t=this;
        	$("#type_select .dropdown li").unbind("click").on("click",function(){
        		var ifmId = $(this).attr("data-ifmId");
        		var ifmName = $(this).attr("data-ifmName");
        		$("#types").text(ifmName).attr('data-ifmId',ifmId);
        	});
		},
		
		bindDepartLiEvent: function(){
        	var t=this;
        	$("#depart_select .dropdown li").unbind("click").on("click",function(){
        		var departId = $(this).attr("data-departid");
        		var departName = $(this).attr("data-departName");
        		$("#departments").text(departName).attr('data-departId',departId);
        		$("#leaders").text("-请选择-").removeAttr('data-departId');
        	});
		},
		bindLeaderLiEvent: function(){
        	var t=this;
        	$("#leader_select .dropdown li").unbind("click").on("click",function(){
        		var userId = $(this).attr("data-userId");
        		var userName = $(this).attr("data-userName");
        		$("#leaders").text(userName).attr('data-userId',userId);
        	});
		},
		
		bindGradeLiEvent: function(){
        	var t=this;
        	$("#grade_select .dropdown li").unbind("click").on("click",function(){
        		var grade = $(this).attr("data-grade");
        		var level = $(this).attr("data-level");
        		$("#grades").text(level).attr('data-grade',grade);
        	});
		}
        
    };
    module.exports = submitObj;
});

